# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     utils
   Description : 数据字典帮助函数
   Author :       Flyoung
   date：          2023/2/22
-------------------------------------------------
   Change Activity:
                   2023/2/22:
-------------------------------------------------
"""
from compiler.lexical.lib import *
from typing import AnyStr, Callable


def is_empty_str(s: AnyStr) -> bool:
    return s is not None and s == ''


def empty_check(func: Callable):
    def wrapper(s: AnyStr):
        if is_empty_str(s):
            return False
        else:
            return func(s)

    return wrapper


@empty_check
def is_underline(c: AnyStr) -> bool:
    return c == underline


@empty_check
def is_digit(c: AnyStr) -> bool:
    return c in digit


@empty_check
def is_alpha(c: AnyStr) -> bool:
    return c in alpha


@empty_check
def is_blank(c: AnyStr) -> bool:
    return c in blank


@empty_check
def is_sign(c: AnyStr) -> bool:
    return c in signs


@empty_check
def is_notes(s: AnyStr) -> bool:
    return s == notes


@empty_check
def is_reserved_word(s: AnyStr) -> bool:
    return s in reserved_words


@empty_check
def is_var_type_reserved_words(s: AnyStr) -> bool:
    return s in var_type_reserved_words


@empty_check
def is_equal_sign(s: AnyStr) -> bool:
    return s == '='


@empty_check
def is_constant(s: AnyStr) -> bool:
    try:
        float(s)
        return True
    except ValueError:
        return False


@empty_check
def is_user_defined(s: AnyStr) -> bool:
    return sum([
        is_sign(s),
        is_reserved_word(s)
    ]) == 0


def get_sign_encode(s: AnyStr) -> int:
    if s in signs:
        return signs[s]
    else:
        return -1


def contain_alpha(s: AnyStr) -> bool:
    return sum([
        is_alpha(i) for i in s
    ]) > 0
